import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'

// 创建一个异步 thunk
export const getMenuList = createAsyncThunk('data/fetchData', async () => {
  return new Promise((resolve, reject) => {
    const routeList = [{
      id: 1,
      path: '/home'
    }, {
      id: 2,
      path: '/list1'
    }]
    setTimeout(() => {
      resolve(routeList)
    }, 1000)
  })
})

export const userSlice = createSlice({
  name: 'user',
  initialState: {
    menuList: []
  },
  reducers: {
    // getMenuList(state) {
    //   return new Promise((resolve, reject) => {
    //     // const routeList = ['/home', '/list1']
    //     const routeList = [{
    //       id: 1,
    //       path: '/home'
    //     }, {
    //       id: 2,
    //       path: '/list1'
    //     }]
    //     setTimeout(() => {
    //       state.menuList = routeList
    //       resolve(routeList)
    //       console.log('菜单来了', state)
    //     }, 1000)
    //   })
    // }
  },
  extraReducers: (builder) => {
    builder
      .addCase(getMenuList.fulfilled, (state, action) => {
        state.menuList = action.payload

      })
  }
})

// export const { getMenuList } = userSlice.actions
export default userSlice.reducer
